package com.example.myapplication.login;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.acticity.BaseActivity;
import com.example.myapplication.api.Api;
import com.example.myapplication.api.AppConfig;
import com.example.myapplication.api.TitleCallBack;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    private EditText etAcc, etPsw;
    private Button btLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

    }

    @Override
    public void publish(int progress) {

    }

    @Override
    public void change(int position) {

    }

    private void initView() {

        etAcc = findViewById(R.id.et_acc);
        etPsw = findViewById(R.id.et_pwd);
        btLogin = findViewById(R.id.btn_login);
        //获取输入框的内容并去除前后的空格
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String acc = etAcc.getText().toString().trim();
                String psw = etPsw.getText().toString().trim();
                login(acc, psw);
            }
        });
    }

    /**
     * 登录的操作
     *
     * @param acc
     * @param psw
     */
    private void login(String acc, String psw) {

        if (acc.isEmpty()) {
            showToast("账号不能为空！");
        } else if (psw.isEmpty()) {
            showToast("密码不能为空！");
        } else {

            HashMap<String,Object> params = new HashMap<>();
            params.put("mobile", acc);
            params.put("password", psw);
            Api.config(AppConfig.LOGIN,params).postRequest(new TitleCallBack() {
                @Override
                public void onSuccess(final String res) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast(res);
                            showToast("登陆成功");
                        }
                    });
                }

                @Override
                public void onFailure(Exception e) {
                    showToast("登陆成功");

                }
            });

//            //创建okhttpclient
//            OkHttpClient client = new OkHttpClient.Builder().build();
//            Map m = new HashMap();
//            m.put("mobile", acc);
//            m.put("password", psw);
//            JSONObject jsonObject = new JSONObject(m);
//            String jsonStR = jsonObject.toString();
//            RequestBody requestBodyJson = RequestBody
//                    .create(MediaType
//                            .parse("appLication/json;charset=UTF-8"), jsonStR);
//
//            //创建Requst
//            final Request request = new Request.Builder()
//                    .url(AppConfig.BASE_URL + "/app/Login")
//                    .addHeader("contentType", "appLication/json;charset=UTF-8")
//                    .post(requestBodyJson)
//                    .build();
//
//            //创建call回调对象
//            final Call call = client.newCall(request);
//
//            //发起请求
//            call.enqueue(new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                    Log.e("onFailure", e.getMessage());
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    final String result = request.body().toString();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            showToast(result);
//                        }
//                    });
//
//                }
//            });

        }


    }
}